import { http } from "@/utils/http"
import { CommonResponse } from "./common"

/**
 * 试卷管理返回数据格式
 */
export type PaperData = {
  records: {
    id: number
    name: string
    groupName: string
    gmtModified: string
  }[]
  total: number
  pageNum: number
  pageSize: number
}

/**
 * 试卷管理请求参数
 */
export type PaperParams = {
  name?: string
  groupName?: string
  pageNum?: number
  pageSize?: number
}

/**
 * 分页查询试卷
 */
export const getPaperList = (data: PaperParams) => {
  return http.request<CommonResponse<PaperData>>('post', '/api/platform/paper/list', { data })
}

/**
 * 试卷详情数据
 */
export type PaperDetail = {
  id: number
  name: string
  groupName: string
  paperQuestionList: {
    questionId: number
    type: number
    title: string
    quesitionIndex: number
    correctAnswer: string
    analysis: string | null
    score: number
    options: {
      key: string
      content: string
      image: string | null
    }[]
  }[]
}

/**
 * 查询试卷详情
 */
export const getPaperDetail = (id: number) => {
  return http.request<CommonResponse<PaperDetail>>('get', `/api/platform/paper/detail/${id}`)
}

/**
 * 新增试卷参数
 */
export type CreatePaperParams = {
  name: string
  groupName: string
  paperCount: number
  questionCount: number
  singleCount: number
  multipleCount: number
  blankCount: number
  singleScore: number
  multipleScore: number
  blankScore: number
  totalScore: number
  allowDuplicate: boolean
}

/**
 * 新增人工组卷
 */
export type CreateManualPaperParams = {
  name: string
  groupName: string
  questionCount: number
  singleChoiceQuestions: string[]
  multipleChoiceQuestions: string[]
  fillBlankQuestions: string[]
  singleScore: number
  multipleScore: number
  blankScore: number
  totalScore: number
}

/**
 * 新增试卷
 */
export const createPaper = (data: CreatePaperParams) => {
  return http.request<CommonResponse<PaperData>>('post', '/api/platform/paper/create', { data })
}

/**
 * 人工组卷生成
 */
export const generatePaper = (data: CreateManualPaperParams) => {
  return http.request<CommonResponse<PaperData>>('post', '/api/platform/paper/create/manual', { data })
}

/**
 * 编辑试卷参数
 */
export type EditPaperParams = {
  id: number
  name: string
  groupName: string
}

/**
 * 编辑试卷
 */
export const updatePaper = (data: EditPaperParams) => {
  return http.request<CommonResponse<PaperData>>('post', '/api/platform/paper/update', { data })
}

/**
 * 更新试卷详情
 */
export const updatePaperStatus = (id: number, status: number) => {
  return http.request<CommonResponse<PaperData>>('post', `/api/platform/paper/${id}/status/${status}`)
}

/**
 * 查询试卷组下拉
 */
export const getPaperGroupList = () => {
  return http.request<CommonResponse>('get', '/api/platform/paper/group/options')
}